<?php
class D_Adapter_Mysql 
{	
	private $mysql;
	private $sql;
	public function __construct($dbhost, $username, $password, $dbname='',$charset='utf8', $pconnect = false)
	{
		$this->mysql = new D_Statement_Mysql($dbhost, $username, $password, $dbname,$charset, $pconnect);
	}
	public function insert($sql, $bind=array())
	{
		return $this->query($sql, $bind);
	}
	public function update($sql, $bind=array())
	{
//		if(!$this->execute($sql,$bind))
//			throw new MysqlException($sql);
	    return $this->query($sql, $bind);
	}
	public function delete($sql,$bind=array())
	{
		return $this->query($sql, $bind);
	}
	public function fetchRow($sql, $bind=array(),$type='FETCH_ASSOC')
	{	
		
		$this->prepare($sql, $bind);
		if($type == 'FETCH_ASSOC')
		{
			return  $this->mysql->Fetch_assoc($this->sql);
		}
		else
		{	
		
			return $this->mysql->Fetch_num($this->sql);
		}
	
	}
	public function fetchAll($sql, $bind=array(),$type='FETCH_ASSOC')
	{	
			
		$this->prepare($sql, $bind);
		if($type == 'FETCH_ASSOC')
		{
			return  $this->mysql->Fetch_assocs($this->sql);
		}
		else
		{	
			return $this->mysql->Fetch_nums($this->sql);
		}
		
		
	}
	public function query($sql,$bind=array())
	{
	
			$this->prepare($sql, $bind);
			$query = $this->mysql->Query($this->sql);
			if($query) return true;
			else return false;
	
	}
	public function get_num()
	{
		$num_rows = $this->mysql->get_num();
		
		return $num_rows;
	}
	private function prepare($sql,$bind =array())
	{
		if (isset($bind) && !empty($bind) && !is_null($bind) && is_array($bind))
		{	
			if(substr_count($sql,'?') > 0)
			{
				foreach($bind as $value)
				{
					 $sql =str_replace ( '?' ,"'$value'"  , $sql );
				}
			}
			else
			{
				foreach($bind as $key => $value)
				{
					 $sql = str_replace ( ":$key" ,"'$value'"  , $sql);
				}
			}
		}
	
		$this->sql = $sql;
		return $this->sql;
	}
}
?>